package com.vcourse.service.edu.controller.api;

import com.vcourse.commons.result.R;
import com.vcourse.service.base.dto.CourseDto;
import com.vcourse.service.edu.entity.Course;
import com.vcourse.service.edu.service.ChapterService;
import com.vcourse.service.edu.service.CourseService;
import com.vcourse.service.edu.vo.ChapterVo;
import com.vcourse.service.edu.vo.WebCourseQueryVo;
import com.vcourse.service.edu.vo.WebCourseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zengsh123
 * @date 2020-06-07 11:53
 */
@Api("课程")
@RestController
@RequestMapping("/api/edu/course")
public class ApiCourseController {
    @Resource
    private CourseService courseService;
    @Resource
    private ChapterService chapterService;

    @ApiOperation("课程列表")
    @GetMapping("list")
    public R list(
            @ApiParam(value = "查询对象", required = false)
                    WebCourseQueryVo webCourseQueryVo){
        List<Course> courseList = courseService.webSelectList(webCourseQueryVo);
        return  R.ok().data(courseList);
    }

    @ApiOperation("根据ID查询课程")
    @GetMapping("get/{courseId}")
    public R getById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String courseId){

        //查询课程信息和讲师信息
        WebCourseVo webCourseVo = courseService.selectWebCourseVoById(courseId);

        //查询当前课程的章节信息
        List<ChapterVo> chapterVoList = chapterService.nestedList(courseId);

        Map<String, Object> map = new HashMap<>(2);
        map.put("course", webCourseVo);
        map.put("chapterVoList", chapterVoList);
        return R.ok().data(map);
    }

    @ApiOperation("根据课程id查询课程信息，服务间调用")
    @GetMapping("inner/get-course-dto/{courseId}")
    public CourseDto getCourseDtoById(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String courseId){
        return courseService.getCourseDtoById(courseId);
    }

}
